package com.t2t.top.devops.util;

import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import com.t2t.top.devops.model.po.Account;
import org.apache.commons.codec.binary.Hex;

import java.security.SecureRandom;


/**
 * ****************************************************************************
 *
 * @author : chenxushao@gmail.com
 *         ****************************************************************************
 * @ClassName: PasswordHelper
 * @Description:密码加密工具
 */
public class PasswordHelper {

    public static Account encryptPassword(Account account) {
        byte[] bytes = new byte[16];
        SecureRandom secureRandom = new SecureRandom();
        secureRandom.nextBytes(bytes);
        String salt = Hex.encodeHexString(bytes);
        String encodePswd = encrypt(account.getPassword(), salt);
        account.setPassword(encodePswd);
        account.setSalt(salt);
        return account;
    }

    //加密
    public static String encrypt(String value, String salt) {
        String encodeValue = Hashing.md5().newHasher().putString(value + salt, Charsets.UTF_8).hash().toString().toLowerCase();
        return encodeValue;
    }
}